//app.js
App({
  onLaunch: function () {
  
  },
  globalData: {
    userInfo: null,
    // appUrl:'http://yyf.yprhealth.com/',
    appUrl: 'http://www.wogewo.com:8080/',
  },
  uid: '',
  titleHome:"兰律师",//首页分享文案
  titleDetail1:'兰律师',//
  titleDetail2:'兰律师',
  titleRest:'兰律师',
  // 123123
  // 用户信息
  userInfo: {

  },
  request: function (url, dataObj, callback1, callback2, callback3) {
    var that = this;
    if (wx.getStorageSync('uid')) {
      dataObj.uid = wx.getStorageSync('uid');
    }
    wx.request({
      url: that.globalData.appUrl + url,
      data: dataObj,
      success: function (res) {
          callback1(res.data);
      },
      complete: function (res) {
        if (callback3) {
          callback3(res);
        }
      }
    })
  },
  post: function (url, dataObj, callback1, callback2, callback3) {
    var that = this;
    wx.request({
      url: that.globalData.appUrl + url,
      data: dataObj,
      method: 'post',
      header: {
        "Content-Type": "application/json; charset=utf-8" //post请求设置
      },
      success: function (res) {
        if (res.data.errno == 0) {
          callback1(res.data);
        } else {
          if (callback2) {
            callback2(res.data);
          }
        }
      },
      complete: function (res) {
        if (callback3) {
          callback3(res);
        }
      }
    })
  },
  // 登陆注册逻辑
  codeLogin: function () {
    var self = this;
    var p = new Promise(function (resolve, reject) {
      var uid = wx.getStorageSync('uid');
      if (!uid) {
        var url = 'login';
        wx.login({
          success: function (res) {
            var obj = {
              code: res.code,
            }
            wx.request({
              url: self.globalData.appUrl + url,
              data: obj,
              method: 'post',
              header: {
                "Content-Type": "application/json; charset=utf-8" //post请求设置
              },
              success: function (res) {
                if (res.data.errno == 0) {
                  var userInfo = res.data.data;
                  var uid = res.data.data.uid;
                  self.uid=uid;
                  self.userInfo=userInfo;
                  wx.setStorageSync('uid', uid);
                  wx.setStorageSync('userInfo', userInfo);
                  resolve(uid);
                }
              },
            })
          }
        })
      } else {
        self.uid = uid;
        self.userInfo = wx.getStorageSync('userInfo');
        resolve(uid);
      }
    });
    return p;


  },
  // 授权注册
  authuorLogin: function (iv, encryptedData) {
    var self = this;
    var p = new Promise(function (resolve, reject) {
      var uid = wx.getStorageSync('uid');
      var url = 'login';
      wx.login({
        success: function (res) {
          var obj = {
            code: res.code,
            iv: iv,
            encryptedData: encryptedData,
          }
          wx.request({
            url: self.globalData.appUrl + url,
            data: obj,
            method: 'post',
            header: {
              "Content-Type": "application/json; charset=utf-8" //post请求设置
            },
            success: function (res) {
              if (res.data.errno == 0) {
                var userInfo = res.data.data;
                var uid = res.data.data.uid;
                wx.setStorageSync('uid', uid);
                wx.setStorageSync('userInfo', userInfo);
                resolve(userInfo);
              }
            },
          })
        }
      })
    });
    return p;
  },

  getQueryString:function (str,name){
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = str.match(reg);
    if(r!= null)return unescape(r[2]); return null;
  }
})
// 08/10